﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.BodyClass = "easyui-layout";
}
<div id="tbtool" class="gou-toolbar" style="padding:3px;">
    <span>用户名称:</span>
    <input id="searchKey" name="searchKey" maxlength="25" style="line-height:26px;border:1px solid #ccc">
    <a href="javascript:;" class="easyui-linkbutton" iconcls="icon-search" onclick="Search();">查找</a>
    <a href="javascript:void(0);" onclick="access(0);" class="easyui-linkbutton" iconcls="icon-add">新增</a>
</div>
<table id="grid" data-options="region:'center',border:false" class="easyui-datagrid"
       rownumbers="true" fitcolumns="true" singleselect="true" fit="true" pagesize="50"></table>

@section Script{

    <script type="text/javascript">
        $(function () {
            function formatOper(val, row, index) {
                var rek = "";
                var cor = "";
                var opt = "";
                if (row.IsStop) {
                    rek = "启用";
                    cor = "green";
                    opt = '<a href="javascript:void(0);" onclick="updateState(\'' + row.UserId + '\',0,\'' + row.LogonName + '\');">启用</a>&nbsp|';
                }
                else {
                    rek = "禁用";
                    cor = "red";
                    opt = '<a href="javascript:void(0);" onclick="updateState(\'' + row.UserId + '\',1,\'' + row.LogonName + '\');">禁用</a>&nbsp|';
                }
                opt = opt + '&nbsp<a href="javascript:void(0);" onclick="access(\'' + row.UserId + '\');">编辑</a>&nbsp|&nbsp;<a href="javascript:void(0);" onclick="deleteUser(\'' + row.UserId + '\',\'' + row.LogonName + '\');">删除</a>&nbsp';

                return opt ;
            }
           

           function  sexFormat(val, row, index)
          {

                            var showStr = "未知";
                            if(row.Sex==1)
                            {
                                 showStr="男";
                            }
                            else if(row.Sex==2)
                            {
                                 showStr="女";
                             }
                             else
                            {
                                 showStr = "未知";
                             }
                            return "<span>" + showStr + "</span>";
          }

           

            $('#grid').datagrid({
                url: 'GetList?name=' + $("#searchKey").val(),
                columns: [[
                     { field: 'UserId', title: 'UserId', width: 70 }
                     ,{ field: 'LogonName', title: '登录名', width: 150 }
                    , { field: 'ShowName', title: '姓名', width: 120 }
                    , { field: 'PositionTypeName', title: '岗位', width: 100 }
                            , { field: 'Sex', title: '性别', width: 50, formatter: sexFormat}
                      , { field: 'RegTypeName', title: '注册来源', width: 50 }
                      , { field: 'LoginCount', title: '登录次数', width: 50 }
        
                      , {
                        field: 'IsStop', title: '状态', width: 50, formatter: function (val, row, index) {
                            var state = (row.IsStop) ? "禁用" : "启用";
                            return "<span>" + state + "</span>";
                        }
                    }
                    , { field: '_operate', title: '操作', width: 100, align: 'center', formatter: formatOper }
                ]],
                toolbar: '#tbtool',
                pagination: true
            });
            var pager = $('#grid').datagrid('getPager');	// get the pager of datagrid
            pager.pagination({
                showPageList: true,
                pageSize: 50
            });

        });
        function Search() {            
            $('#grid').datagrid("load", {
                name: $("#searchKey").val()
            });
        }

        function deleteUser(id,name) {
            var rek = "删除";
            $.messager.confirm(rek, "你确定" + rek + "”" + name + "“吗?", function (istrue) {
                if (istrue) {
                    var param = { userId: id };
                    $.ajax({
                        "type": "Post",
                        "url": "ajaxDeleteUser?t=" + new Date().getTime(),
                        "dataType": "json",
                        "data": param,
                        "success": function (data) {
                            if (data.IsSuccess) {
                                $.messager.alert('提示', data.Msg);
                                $("#grid").datagrid("reload");
                            }
                            else
                            {
                                $.messager.alert('提示', data.Msg);
                             }
                        }
                    });
                }
            });
           
        }

        function updateState(id, state,name) {
            var rek = state == 1 ? "禁用" : "启用";

            $.messager.confirm(rek, "你确定" + rek + "”" + name + "“吗?", function (istrue) {
                if (istrue) {
                    var param = { userId: id };
                    $.ajax({
                        "type": "Post",
                        "url": "ajaxUpdateUserState?t=" + new Date().getTime(),
                        "dataType": "json",
                        "data": param,
                        "success": function (data) {
                            if (data.IsSuccess) {
                                $.messager.alert('提示', data.Msg);
                                $("#grid").datagrid("reload");
                            }
                            else
                            {
                                $.messager.alert('提示', data.Msg);
                             }
                        }
                    });
                }
            });
        }


        function access(id) {
            window.location.href = "access?userId=" + id;
        }
    </script>
}





